import storage from "store";
import { login, logout } from "@/api/login";
import {ACCESS_TOKEN, USER_INFO, ADMIN, COMPANY_ID, USER_ID, LEGAL_TENDER_KEY} from "@/store/mutation-types";

import { mapActions } from "vuex";
import {resetRouter} from "@/router";

const user = {
  state: {
    token: "",
    name: "",
    welcome: "",
    avatar: "",
    roles: [],
    info: {},
    userInfoNum: {},
    admin: false, //是否是管理员

  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name;
      state.welcome = welcome;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_INFO: (state, info) => {
      state.info = info;
    },
    SET_USER_INFO_NUM: (state, val) => {
      state.userInfoNum = val;
    }
  },

  actions: {
    ...mapActions["GenerateRoutes"],
    // 登录
    Login({ commit,state }, userInfo) {
      return new Promise(async (resolve, reject) => {
        login(userInfo)
          .then(res => {
            console.log("------", res);
            if (res.code == 0) {
              storage.set(
                ACCESS_TOKEN,
                res.data.tokenId,
                7 * 24 * 60 * 60 * 1000
              );
              storage.set(USER_INFO, res.data, 7 * 24 * 60 * 60 * 1000);

              // console.log(res.data.menuList);
              let admin = res.data.menuList.some(item => {
                return item.webUrl == "/admin";
              });
              state.admin= admin
              storage.set(ADMIN,admin);
            }
            resolve(res);
          })
          .catch(error => {
            reject(error);
          });
      });
    },

    // 获取用户信息
    // GetInfo({ commit }) {
    //   return new Promise((resolve, reject) => {
    //     getInfo().then(response => {
    //       const result = response.result

    //       if (result.role && result.role.permissions.length > 0) {
    //         const role = result.role
    //         role.permissions = result.role.permissions
    //         role.permissions.map(per => {
    //           if (per.actionEntitySet != null && per.actionEntitySet.length > 0) {
    //             const action = per.actionEntitySet.map(action => { return action.action })
    //             per.actionList = action
    //           }
    //         })
    //         role.permissionList = role.permissions.map(permission => { return permission.permissionId })
    //         commit('SET_ROLES', result.role)
    //         commit('SET_INFO', result)
    //       } else {
    //         reject(new Error('getInfo: roles must be a non-null array !'))
    //       }

    //       commit('SET_NAME', { name: result.name, welcome: welcome() })
    //       commit('SET_AVATAR', result.avatar)

    //       resolve(response)
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    Logout({ commit, state }) {
      return new Promise(resolve => {
        logout(state.token)
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            /*storage.remove(ACCESS_TOKEN);
            storage.remove(USER_INFO);
            storage.remove(ADMIN);
            storage.remove(COMPANY_ID);
            storage.remove(USER_ID);
            storage.remove(LEGAL_TENDER_KEY);*/

            localStorage.removeItem(ACCESS_TOKEN)
            localStorage.removeItem(USER_INFO)
            localStorage.removeItem(ADMIN)
            localStorage.removeItem(COMPANY_ID)
            localStorage.removeItem(USER_ID)
            localStorage.removeItem(LEGAL_TENDER_KEY)
            resetRouter()
            resolve();
          })
          .catch(() => {
            resolve();
          })
          .finally(() => {});
      });
    }
  }
};

export default user
